package com.xingfudazi.app.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.xingfudazi.app.domain.AppMoment;
import com.xingfudazi.app.mapper.AppMomentMapper;
import com.xingfudazi.app.service.IAppMomentService;
import com.xingfudazi.common.enums.DBDataStatus;
import com.xingfudazi.common.utils.SecurityUtils;
import com.xingfudazi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 动态Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-21
 */
@Service
public class AppMomentServiceImpl extends ServiceImpl<AppMomentMapper, AppMoment> implements IAppMomentService {
    @Autowired
    private AppMomentMapper appMomentMapper;

    /**
     * 查询动态
     *
     * @param id 动态主键
     * @return 动态
     */
    @Override
    public AppMoment selectAppMomentById(Long id) {
        return appMomentMapper.selectById(id);
    }

    /**
     * 查询动态列表
     *
     * @param appMoment 动态
     * @return 动态
     */
    @Override
    public List<AppMoment> selectAppMomentList(AppMoment appMoment) {
        LambdaQueryWrapper<AppMoment> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(AppMoment::getCreateTime);
        return appMomentMapper.selectList(wrapper);
    }

    /**
     * 新增动态
     *
     * @param appMoment 动态
     * @return 结果
     */
    @Override
    public int insertAppMoment(AppMoment appMoment) {
        Long userId = SecurityUtils.getUserId();
        appMoment.setNid(userId);
        appMoment.setCreateTime(null);
        appMoment.setId(null);
        appMoment.setCommentNum(0);
        appMoment.setLikeNum(0);
        return appMomentMapper.insert(appMoment);
    }

    /**
     * 修改动态
     *
     * @param appMoment 动态
     * @return 结果
     */
    @Override
    public int updateAppMoment(AppMoment appMoment) {
        return appMomentMapper.updateById(appMoment);
    }

    /**
     * 删除动态信息
     *
     * @param id 动态主键
     * @return 结果
     */
    @Override
    public int deleteAppMomentById(Long id) {
        AppMoment appMoment = new AppMoment();
        appMoment.setDelFlag(DBDataStatus.DELETED.getCode());
        appMoment.setId(id);
        return appMomentMapper.updateById(appMoment);
    }

    @Override
    public List<AppMoment> turn(Integer pageSize, Long lastId, String search) {
        LambdaQueryWrapper<AppMoment> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(AppMoment::getCreateTime);
        wrapper.eq(AppMoment::getDelFlag, DBDataStatus.OK.getCode());

        if (StringUtils.isNotBlank(search)) {
            wrapper.like(AppMoment::getContent, search);
        }

        if (lastId != null) {
            wrapper.lt(AppMoment::getId, lastId);
        }

        PageHelper.startPage(1, pageSize);
        return this.appMomentMapper.selectList(wrapper);
    }

    @Override
    public void like(Long mid, int num) {
        if (mid == null) {
            return;
        }
        UpdateWrapper<AppMoment> updateWrapper = new UpdateWrapper<>();
        if (num > 0) {
            updateWrapper.setSql("like_num = like_num + " + num);

        } else {
            updateWrapper.setSql("like_num = like_num -" + Math.abs(num));
        }
        updateWrapper.eq("id", mid);
        this.update(updateWrapper);
    }

    @Override
    public void comment(Long mid, int num) {
        if (mid == null) {
            return;
        }
        UpdateWrapper<AppMoment> updateWrapper = new UpdateWrapper<>();
        if (num > 0) {
            updateWrapper.setSql("comment_num = comment_num + " + num);

        } else {
            updateWrapper.setSql("comment_num = comment_num -" + num);
        }
        updateWrapper.eq("id", mid);
        this.update(updateWrapper);
    }
}
